package com.start.quartz.mappers;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.mapper.BaseMapper;
import com.baomidou.mybatisplus.core.toolkit.Constants;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.start.quartz.entity.StartQuartzEntity;
import com.start.quartz.model.StartQuartzEntityModel;
import org.apache.ibatis.annotations.Mapper;
import org.apache.ibatis.annotations.Param;
import org.apache.ibatis.annotations.Select;

@Mapper
public interface StartQuartzMapper extends BaseMapper<StartQuartzEntity> {
    String QUERY_QUARTZ = "SELECT a.* FROM (  " +
            "SELECT  " +
            "  sq.*,  " +
            "  from_unixtime( qt.START_TIME / 1000 ) AS start_time,  " +
            "  from_unixtime( qt.PREV_FIRE_TIME / 1000 ) AS prev_fire_time,  " +
            "  from_unixtime( qt.NEXT_FIRE_TIME / 1000 ) AS next_fire_time,  " +
            "  from_unixtime( qt.END_TIME / 1000 ) AS end_time,  " +
            "  qt.TRIGGER_STATE AS trigger_state   " +
            "FROM  " +
            "  start_quartz sq  " +
            "  LEFT JOIN qrtz_triggers qt ON qt.JOB_NAME = sq.id   " +
            "  AND sq.group_key = qt.JOB_GROUP ) a ";

    @Select(QUERY_QUARTZ + "  ${ew.customSqlSegment}")
    Page<StartQuartzEntityModel> pageQuartzEntityModel(@Param("page") Page<StartQuartzEntityModel> page,
                                                       @Param(Constants.WRAPPER) QueryWrapper<StartQuartzEntityModel> queryWrapper);

    @Select(QUERY_QUARTZ + " where id=#{id}")
    StartQuartzEntityModel getQuartById(@Param("id") String id);
}
